package com.bestom.bluetoothlock.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.bestom.bluetoothlock.LockApplication;
import com.bestom.bluetoothlock.R;
import com.bestom.bluetoothlock.bluetooth.BleServiceManage;
import com.bestom.bluetoothlock.bluetooth.BraceletInstructions;
import com.bestom.bluetoothlock.entity.DeviceInfo;
import com.bestom.bluetoothlock.entity.ReceiveInfo;
import com.bestom.bluetoothlock.util.Constants;
import com.bestom.bluetoothlock.util.CurrencyDataManage;
import com.bestom.bluetoothlock.util.LogMsg;
import com.bestom.bluetoothlock.util.ToastUtil;
import com.bestom.bluetoothlock.views.InputDialog;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterFingerprintActivity extends BaseActivity {
    private static final int HANDLER_MESSAGE_REGISTERING_UI = 2;
    private static final int HANDLER_MESSAGE_REGISTER_FAIL = 1;
    private static final int HANDLER_MESS_RECEIVE = 0;
    /* access modifiers changed from: private */
    public BleServiceManage bleManage;
    /* access modifiers changed from: private */
    public String current = "0";
    /* access modifiers changed from: private */
    public DeviceInfo deviceInfo;
    @SuppressLint("HandlerLeak")
    public Handler handler = new Handler() {
        public void handleMessage(Message message) {
            super.handleMessage(message);
            switch (message.what) {
            }
        }
    };
    @SuppressLint("HandlerLeak")
    private Handler handlerListen = new Handler() {

        public void handleMessage(Message message) {
            super.handleMessage(message);
            int i = message.what;
            if (i != 1) {
                if (i == 3) {
                    ReceiveInfo receiveInfo = (ReceiveInfo) message.obj;
                    String cmd = receiveInfo.getCmd();
                    String result = receiveInfo.getResult();
                    Log.d("agui", "cmd:" + cmd + " result:" + result);
                    if (cmd.equals(BraceletInstructions.INSTRUCTIONS_FINGER_AUTO_REGISTER)) {
                        LogMsg.Mess("auto register result:" + result);
                    } else if (cmd.equals(BraceletInstructions.INSTRUCTIONS_CANCEL_FINGER_REGISTER)) {
                        boolean equals = result.equals("00");
                    } else {
                        boolean z = false;
                        if (cmd.equals(BraceletInstructions.INSTRUCTIONS_FINGER_REGISTER_RESULT)) {
                            if (result.equals("00")) {
                                String value = receiveInfo.getValue();
                                Log.d("agui", "progress:" + value);
                                RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_success_please_register_again));
                                if (value.equals(BraceletInstructions.INSTRUCTIONS_SET_MACHINERY_PARM) || value.equals(BraceletInstructions.INSTRUCTIONS_SYNC_TIME)) {
                                    String unused = RegisterFingerprintActivity.this.current = BraceletInstructions.INSTRUCTIONS_FINGER_REGISTER;
                                    RegisterFingerprintActivity.this.progressImg.setImageResource(R.drawable.print1);
                                    int unused2 = RegisterFingerprintActivity.this.progress = 10;
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_success_please_register_again));
                                } else if (value.equals("22") || value.equals("21")) {
                                    String unused3 = RegisterFingerprintActivity.this.current = BraceletInstructions.INSTRUCTIONS_CANCEL_FINGER_REGISTER;
                                    RegisterFingerprintActivity.this.progressImg.setImageResource(R.drawable.print2);
                                    int unused4 = RegisterFingerprintActivity.this.progress = 20;
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_success_please_register_again));
                                } else if (value.equals("33") || value.equals("32")) {
                                    String unused5 = RegisterFingerprintActivity.this.current = BraceletInstructions.INSTRUCTIONS_RESET_FINGER;
                                    RegisterFingerprintActivity.this.progressImg.setImageResource(R.drawable.print3);
                                    int unused6 = RegisterFingerprintActivity.this.progress = 40;
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_success_please_register_again));
                                } else if (value.equals("44") || value.equals("42") || value.equals("43")) {
                                    String unused7 = RegisterFingerprintActivity.this.current = BraceletInstructions.INSTRUCTIONS_FINGER_NUMBER;
                                    RegisterFingerprintActivity.this.progressImg.setImageResource(R.drawable.print4);
                                    int unused8 = RegisterFingerprintActivity.this.progress = 60;
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_success_please_register_again));
                                } else if (value.equals("55") || value.equals("54") || value.equals("53")) {
                                    String unused9 = RegisterFingerprintActivity.this.current = BraceletInstructions.INSTRUCTIONS_POWER;
                                    RegisterFingerprintActivity.this.progressImg.setImageResource(R.drawable.print5);
                                    int unused10 = RegisterFingerprintActivity.this.progress = 80;
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_success_please_register_again));
                                } else if (value.equals("64")) {
                                    String id = receiveInfo.getId();
                                    RegisterFingerprintActivity.this.progressImg.setImageResource(R.drawable.print6);
                                    Log.d("agui", "save:" + id);
                                    String unused11 = RegisterFingerprintActivity.this.current = "0";
                                    int unused12 = RegisterFingerprintActivity.this.progress = 100;
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.fingerprint_entry_finish));
                                    RegisterFingerprintActivity.this.sendCmd(BraceletInstructions.saveFingerInstructions(id));
                                }
                            } else if (result.equals(BraceletInstructions.INSTRUCTIONS_CANCEL_FINGER_REGISTER) || result.equals(BraceletInstructions.INSTRUCTIONS_DELETE_FINGER)) {
                                RegisterFingerprintActivity.this.stopRegister();
                                RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_operation_improper_please_anew_register));
                            } else if (!result.equals(BraceletInstructions.INSTRUCTIONS_RESET_FINGER)) {
                                if (result.equals(BraceletInstructions.INSTRUCTIONS_FINGER_NUMBER) || result.equals(BraceletInstructions.INSTRUCTIONS_POWER) || result.equals(BraceletInstructions.INSTRUCTIONS_BLE_STRENGTH) || result.equals(BraceletInstructions.INSTRUCTIONS_FINGER_REGISTER_RESULT)) {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_invalid));
                                } else if (result.equals("0b")) {
                                    RegisterFingerprintActivity.this.stopRegister();
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_full));
                                    RegisterFingerprintActivity.this.tv_hint.setVisibility(View.VISIBLE);
                                } else if (result.equals("0c") || result.equals("0d")) {
                                    RegisterFingerprintActivity.this.stopRegister();
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.fingerprint_entering_fail));
                                    RegisterFingerprintActivity.this.tv_hint.setVisibility(View.VISIBLE);
                                } else if (result.equals("0e") || result.equals(BraceletInstructions.INSTRUCTIONS_SET_MACHINERY_PARM) || result.equals(BraceletInstructions.INSTRUCTIONS_SYNC_TIME) || result.equals(BraceletInstructions.INSTRUCTIONS_GET_RECORD)) {
                                    RegisterFingerprintActivity registerFingerprintActivity = RegisterFingerprintActivity.this;
                                    registerFingerprintActivity.sendCmd(BraceletInstructions.fingerRegisterInstructions(registerFingerprintActivity.current));
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.fingerprint_gather_fail));
                                } else if (result.equals(BraceletInstructions.INSTRUCTIONS_FINGER_AUTO_REGISTER)) {
                                    RegisterFingerprintActivity.this.stopRegister();
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.force_to_exit));
                                    RegisterFingerprintActivity.this.tv_hint.setVisibility(View.VISIBLE);
                                } else if (result.equals("0f")) {
                                    RegisterFingerprintActivity.this.progressImg.setImageResource(R.drawable.print0);
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_has_been_recorded));
                                    RegisterFingerprintActivity.this.tv_hint.setVisibility(View.VISIBLE);
                                } else if (result.equals("ff")) {
                                    RegisterFingerprintActivity.this.stopRegister();
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.other_error));
                                    RegisterFingerprintActivity.this.tv_hint.setVisibility(View.VISIBLE);
                                }
                            }
                        } else if (!cmd.equals("0a")) {
                            if (cmd.equals("0b")) {
                                if (result.equals("00")) {
                                    LogMsg.Mess("发送查询指纹保存结果");
                                    RegisterFingerprintActivity.this.sendCmd(BraceletInstructions.saveFingerResultInstructions());
                                    return;
                                }
                                LogMsg.Mess("指纹保存失败");
                            } else if (cmd.equals("0c") && RegisterFingerprintActivity.this.isRegiser) {
                                boolean unused13 = RegisterFingerprintActivity.this.isRegiser = false;
                                RegisterFingerprintActivity.this.tv_register.setText(RegisterFingerprintActivity.this.getString(R.string.restart_register_fingerprint));
                                if (result.equals("00")) {
                                    String id2 = receiveInfo.getId();
                                    int i2 = LockApplication.getSharedPreferencesHelper().getInt("finger_count_" + CurrencyDataManage.getInstance().getCurrentBleDevice().getLockmac());
                                    if (i2 > 0) {
                                        int i3 = 0;
                                        while (true) {
                                            if (i3 >= i2) {
                                                break;
                                            }
                                            if (id2.equals(LockApplication.getSharedPreferencesHelper().getString("finger_" + CurrencyDataManage.getInstance().getCurrentBleDevice().getLockmac() + "_id" + i3))) {
                                                z = true;
                                                break;
                                            }
                                            i3++;
                                        }
                                    }
                                    if (!z) {
                                        LockApplication.getSharedPreferencesHelper().saveString("finger_" + CurrencyDataManage.getInstance().getCurrentBleDevice().getLockmac() + "_id" + i2, id2);
                                        StringBuilder sb = new StringBuilder();
                                        sb.append("finger_count_");
                                        sb.append(CurrencyDataManage.getInstance().getCurrentBleDevice().getLockmac());
                                        LockApplication.getSharedPreferencesHelper().saveInt(sb.toString(), i2 + 1);
                                    }
                                    RegisterFingerprintActivity registerFingerprintActivity2 = RegisterFingerprintActivity.this;
                                    registerFingerprintActivity2.showInputDialog(registerFingerprintActivity2.getString(R.string.set_fingerprint_name), id2);
                                    LogMsg.Mess("指纹保存结果成功");
                                } else if (result.equals(BraceletInstructions.INSTRUCTIONS_RESET_FINGER)) {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_system_busy));
                                    LogMsg.Mess("查询保存结果繁忙");
                                    ToastUtil.toast(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_system_busy));
                                    RegisterFingerprintActivity.this.sendCmd(BraceletInstructions.saveFingerResultInstructions());
                                } else if (result.equals(BraceletInstructions.INSTRUCTIONS_FINGER_NUMBER)) {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_system_busy));
                                    LogMsg.Mess("没有发送该命令的请求，就查询结果");
                                    RegisterFingerprintActivity.this.sendCmd(BraceletInstructions.saveFingerInstructions(receiveInfo.getId()));
                                } else if (result.equals(BraceletInstructions.INSTRUCTIONS_POWER)) {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_system_busy));
                                    LogMsg.Mess("系统软件上报错误");
                                    RegisterFingerprintActivity.this.sendCmd(BraceletInstructions.saveFingerResultInstructions());
                                } else if (result.equals(BraceletInstructions.INSTRUCTIONS_DELETE_FINGER)) {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_system_busy));
                                    LogMsg.Mess("硬件错误");
                                    RegisterFingerprintActivity.this.sendCmd(BraceletInstructions.saveFingerResultInstructions());
                                } else if (result.equals("0c")) {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_system_busy));
                                    LogMsg.Mess("存储写入失败 ");
                                    RegisterFingerprintActivity.this.sendCmd(BraceletInstructions.saveFingerResultInstructions());
                                } else if (result.equals("0d")) {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_system_busy));
                                    LogMsg.Mess("存储读取失败 ");
                                    RegisterFingerprintActivity.this.sendCmd(BraceletInstructions.saveFingerResultInstructions());
                                } else if (result.equals("0b")) {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_fingerprint_full));
                                    LogMsg.Mess("存储空间满 ");
                                } else if (result.equals("0f")) {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_has_been_recorded));
                                    LogMsg.Mess("指纹模板重复 ");
                                } else {
                                    RegisterFingerprintActivity.this.tv_hint.setText(RegisterFingerprintActivity.this.getString(R.string.register_has_been_recorded));
                                    LogMsg.Mess("查询保存结果失败");
                                }
                            }
                        }
                    }
                }
            } else if (!((Boolean) message.obj).booleanValue()) {
                ToastUtil.toast(RegisterFingerprintActivity.this.getString(R.string.device_disconnect));
                RegisterFingerprintActivity.this.bleManage.connect(RegisterFingerprintActivity.this.deviceInfo);
                RegisterFingerprintActivity.this.finish();
            }
        }
    };
    private boolean isBLEMode = false;
    /* access modifiers changed from: private */
    public boolean isRegiser = false;
    private boolean isRepulse = false;
    @BindView(R.id.iv_left)
    ImageView iv_left;
    /* access modifiers changed from: private */
    public int progress = 0;
    @BindView(R.id.loading)
    ImageView progressImg;
    private Runnable task = new Runnable() {
        public void run() {
            RegisterFingerprintActivity.this.bleManage.sendData(BraceletInstructions.fingerRegisterResultInstructions());
            RegisterFingerprintActivity.this.handler.postDelayed(this, 300);
        }
    };
    @BindView(R.id.tv_hint)
    TextView tv_hint;
    @BindView(R.id.tv_register)
    TextView tv_register;
    @BindView(R.id.tv_title)
    TextView tv_title;

    /* access modifiers changed from: protected */
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_register_fingerprint);
        ButterKnife.bind((Activity) this);
        this.deviceInfo = (DeviceInfo) getIntent().getSerializableExtra(Constants.KEY_VALUE);
        initView();
    }

    private void initView() {
        this.tv_title.setText(getString(R.string.register_fingerprint));
        this.iv_left.setVisibility(View.VISIBLE);
        this.bleManage = BleServiceManage.getInstance(this);
        this.bleManage.setHandlerListener(this.handlerListen);
    }

    /* access modifiers changed from: private */
    public void showInputDialog(String str, final String str2) {
        final InputDialog inputDialog = new InputDialog(this);
        inputDialog.show();
        inputDialog.setTitle(str);
        inputDialog.setOnConfirmClickListener(new InputDialog.OnConfirmClickListener() {
            public void onConfirmClick(View view) {
                String content = inputDialog.getContent();
                if (TextUtils.isEmpty(content)) {
                    ToastUtil.toast(RegisterFingerprintActivity.this.getString(R.string.please_input_fingerprint_name));
                    return;
                }
                LockApplication.getSharedPreferencesHelper().saveString("finger_" + CurrencyDataManage.getInstance().getCurrentBleDevice().getLockmac() + "_name" + str2, content);
            }
        });
    }

    /* access modifiers changed from: private */
    public void sendCmd(String str) {
        this.bleManage.sendData(str);
    }

    private void startRegister() {
        this.tv_hint.setText(getString(R.string.register_fingerprint_hint));
        this.progressImg.setImageResource(R.drawable.print0);
        this.tv_register.setText(getString(R.string.stop_register_fingerprint));
        this.tv_hint.setVisibility(View.VISIBLE);
        this.isRegiser = true;
    }

    /* access modifiers changed from: private */
    public void stopRegister() {
        this.tv_hint.setVisibility(View.INVISIBLE);
        this.tv_register.setText(getString(R.string.restart_register_fingerprint));
        this.progress = 0;
        this.current = "0";
        this.progressImg.setImageResource(R.drawable.print0);
        this.isRegiser = false;
    }


    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i == 4) {
            if (this.isRegiser) {
                ToastUtil.toast(getString(R.string.please_cancel_register_fingerprint));
                LogMsg.Mess("先取消注册");
            } else {
                finish();
            }
        }
        return super.onKeyDown(i, keyEvent);
    }

    /* access modifiers changed from: protected */
    public void onResume() {
        super.onResume();
    }

    /* access modifiers changed from: package-private */
    @OnClick({R.id.iv_left, R.id.tv_register})
    public void onClickView(View view) {
        int id = view.getId();
        if (id == R.id.iv_left) {
            if (this.isRegiser) {
                sendCmd(BraceletInstructions.cancelFingerRegisterInstructions());
            }
            finish();
        } else if (id == R.id.tv_register) {
            if (!this.bleManage.isConnect()) {
                ToastUtil.toast(getString(R.string.not_connected_bluetooth));
            } else if (this.isRegiser) {
                stopRegister();
                sendCmd(BraceletInstructions.cancelFingerRegisterInstructions());
            } else {
                startRegister();
                this.current = BraceletInstructions.INSTRUCTIONS_CHANGE_LOCK_STATE;
                sendCmd(BraceletInstructions.fingerRegisterInstructions());
            }
        }
    }
}
